import fs from 'fs';
import ExcelJS from 'exceljs';
import path from 'path';
import util from "util";
import saveFile from "./保存文件工具.js"
import  dayjs from "dayjs"

let text = await util.promisify(fs.readFile)("./题目文本.txt", 'utf8')

let list=text.split("\n\r")
let questionList=[]


for (let item of list) {
    let aar=(item+"\n").split("\r\n")
    try {
        questionList.push({
            题型: "单选题",
            标题: aar[0].split(". ")[1],
            分数: 5,
            解析: aar[6],
            正确答案: aar[5].split("：")[1],
            选项A: aar[1].split(". ")[1],
            选项B: aar[2].split(". ")[1],
            选项C: aar[3].split(". ")[1],
            选项D: aar[4].split(". ")[1],
            // type:aar[7]||"",
            // level:aar[8]||"",
            // number:aar[9]||"",
        })
    } catch (e) {
        console.log(e);
    }
}
console.log(questionList);


let  saveJsonFile=()=>{
    saveFile("./question_data.json",questionList)
}


let  saveExcel=()=>{
// 创建一个新的工作簿
    const workbook = new ExcelJS.Workbook();
// 添加一个工作表
    const worksheet = workbook.addWorksheet('数据');
    worksheet.addRow(Object.keys(questionList[0]));
    questionList.forEach(item=>{
        worksheet.addRow( Object.values(item));

    })

// 保存工作簿为 Excel 文件
    workbook.xlsx.writeFile(`数据${dayjs().format("MM-DD")}.xlsx`)
        .then(() => {
            console.log('ok');
        })
        .catch((error) => {
            console.error('Error creating Excel file:', error);
        });
}
saveExcel()